//
//  MagnifyNavAnimation.m
//  TheVideoClip
//
//  Created by huyan on 2018/4/2.
//  Copyright © 2018年 北斗君合MacMini. All rights reserved.
//

#import "MagnifyNavAnimation.h"

@interface MagnifyNavAnimation () <CAAnimationDelegate>

//@property (nonatomic, weak) id <UIViewControllerContextTransitioning> transitionContext;

@end

@implementation MagnifyNavAnimation

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.5;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (_transitionType == UINavigationControllerOperationPush) {
        [self pushAnimation:transitionContext];
    } else {
        [self popAnimation:transitionContext];
    }
}

- (void)pushAnimation:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toView];
    //画两个路径
    UIBezierPath *startCycle = [UIBezierPath bezierPathWithRoundedRect:_controlRect cornerRadius:_cornerRadius];
    CGRect bigCycleRect = [self getBigCycleRect];
    CGFloat endCornerRadius = (bigCycleRect.size.height / _controlRect.size.height) * _cornerRadius;
    UIBezierPath *endCycle = [UIBezierPath bezierPathWithRoundedRect:bigCycleRect cornerRadius:endCornerRadius];
    //创建CAShapeLayer进行遮盖
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    //设置layer的path保证动画后layer不会回弹
    maskLayer.path = endCycle.CGPath;
    //将maskLayer作为toVC.View的遮盖
    toView.layer.mask = maskLayer;
    //创建路径动画
    CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    maskLayerAnimation.delegate = self;
    //动画是加到layer上的，所以必须为CGPath，再将CGPath桥接为OC对象
    maskLayerAnimation.fromValue = (__bridge id)(startCycle.CGPath);
    maskLayerAnimation.toValue = (__bridge id)((endCycle.CGPath));
    maskLayerAnimation.duration = [self transitionDuration:transitionContext];
    maskLayerAnimation.delegate = self;
    //设置淡入淡出
    maskLayerAnimation.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [maskLayerAnimation setValue:transitionContext forKey:@"transitionContext"];
    [maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
}

- (void)popAnimation:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    UIView *containerView = [transitionContext containerView];
    [containerView insertSubview:toView atIndex:0];
    //画两个路径
    CGRect bigCycleRect = [self getBigCycleRect];
    CGFloat endCornerRadius = (bigCycleRect.size.height / _controlRect.size.height) * _cornerRadius;
    UIBezierPath *startCycle = [UIBezierPath bezierPathWithRoundedRect:bigCycleRect cornerRadius:endCornerRadius];
    UIBezierPath *endCycle = [UIBezierPath bezierPathWithRoundedRect:_controlRect cornerRadius:_cornerRadius];
    //创建CAShapeLayer进行遮盖
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.fillColor = [UIColor greenColor].CGColor;
    maskLayer.path = endCycle.CGPath;
    fromView.layer.mask = maskLayer;
    //创建路径动画
    CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    maskLayerAnimation.delegate = self;
    maskLayerAnimation.fromValue = (__bridge id)(startCycle.CGPath);
    maskLayerAnimation.toValue = (__bridge id)((endCycle.CGPath));
    maskLayerAnimation.duration = [self transitionDuration:transitionContext];
    maskLayerAnimation.delegate = self;
    maskLayerAnimation.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [maskLayerAnimation setValue:transitionContext forKey:@"transitionContext"];
    [maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
}

- (CGRect)getBigCycleRect {
    CGFloat centerX = _controlRect.origin.x + _controlRect.size.width * 0.5;
    CGFloat centerY = _controlRect.origin.y + _controlRect.size.height * 0.5;
    CGFloat endHeight = 0;
    if (centerY > kScreen_Height - centerY) {
        endHeight = centerY * 2;
    } else {
        endHeight = (kScreen_Height - centerY) * 2;
    }
    CGFloat endWidth = endHeight * (_controlRect.size.width / _controlRect.size.height);
    CGFloat endX = centerX - endWidth * 0.5;
    CGFloat endY = centerY - endHeight * 0.5;
    return CGRectMake(endX, endY, endWidth, endHeight);
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    id <UIViewControllerContextTransitioning> transitionContext = [anim valueForKey:@"transitionContext"];
    [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    [transitionContext viewControllerForKey:UITransitionContextToViewKey].view.layer.mask = nil;
}

@end
